<?php
include(__DIR__ . '/config.php');

use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;


//设置你的连接
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$channel = $conn->channel();
$queue = 'test.queue.lu';

/*
    name: $queue
    passive: false
    durable: true // the queue will survive server restarts
    exclusive: false // the queue can be accessed in other channels
    auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$channel->queue_declare($queue, false, true, false, false);

try {
    while ($envelope = $channel->basic_get()) {
        
        $queue_name = '';//$envelope->delivery_info['queue_name'];
        $key = $envelope->delivery_info['routing_key'];
        $payload = $envelope->body;

        echo '1.'.$queue_name.
            '</br>'.
            '2.'.$key.
            '</br>'.
            '3.'.$payload.
            '</br></br></br>';

        //do work, eg: insert data to DB
        $status = false;

        if ($status) {
            // ACK and drop message if retry more than limit number
            $channel->basic_ack($envelope->delivery_info['delivery_tag']);
        } else {

            // NACK and requeue message if failed to retry
            $channel->basic_nack($envelope->delivery_info['delivery_tag'], false, true);
        }
    }
} catch (Exception $e) {
    echo $e->getMessage();
    //$this->logger->logError('process_queue', $e);
}


$channel->close();
$conn->close();

?>